<template>
  <div id="userRegisterPage">
    <a-form
      style="max-width: 480px; margin: 0 auto"
      :model="formState"
      class="loginRegister"
      name="basic"
      label-align="left"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 20 }"
      autocomplete="off"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <h2 class="title">用户注册</h2>
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: 'Please input your userAccount!' }]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入账号"
        />
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: 'Please input your password!' },
          { min: 6, message: '密码长度不能小于6位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item
        label="确认密码"
        name="checkPassword"
        :rules="[
          { required: true, message: 'Please input your password!' },
          { min: 6, message: '请再次输入密码' },
        ]"
      >
        <a-input-password
          v-model:value="formState.checkPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item
        label="编号"
        name="planetCode"
        :rules="[{ required: true, message: 'Please input your password!' }]"
      >
        <a-input-password
          v-model:value="formState.planetCode"
          placeholder="编号"
        />
      </a-form-item>

      <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
        <a-checkbox v-model:checked="formState.remember"
          >Remember me</a-checkbox
        >
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 4, span: 20 }">
        <a-button type="primary" html-type="submit" block size="large"
          >注册</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from "vue";
import { userLogin, userRegister } from "@/api/user";
import { userLoginUserStore } from "@/store/userLoginUserStore";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";

interface FormState {
  userAccount: string;
  userPassword: string;
  checkPassword: string;
  planetCode: string;
}

const formState = reactive<FormState>({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
  planetCode: "",
});
const router = useRouter();
const onFinish = async (values: any) => {
  //判断两次密码是否一致
  if (formState.userPassword !== formState.checkPassword) {
    message.error("两次密码不一致");
    return;
  }
  //console.log("Success:", values);
  const res = await userRegister(values);
  //登录成功后，保存到全局状态中
  if (res.data.code == 0 && res.data.data) {
    message.success("注册成功");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("注册失败" + res.data.description);
  }
};

const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};
</script>
<style scoped>
#userRegisterPage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  .title {
    text-align: center;
    margin-bottom: 16px;
  }
  .loginRegister {
    width: 100%;
    max-width: 400px;
    padding: 2rem;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}
</style>
